unit Unit1;

interface

uses
  Classes, SysUtils, IWAppForm, IWApplication, IWColor, IWTypes,
  IWVCLBaseControl, IWBaseControl, IWBaseHTMLControl, IWControl, IWCompButton,
  Controls, Forms, IWVCLBaseContainer, IWContainer, IWHTMLContainer,
  IWHTML40Container, IWRegion, IWCompListbox, IWHTMLControls, Unit2,
  IWCompObject, IWCompMPEG, IWCompLabel, DB, IWGrids, IWDBGrids, IWVCLComponent,
  IWBaseLayoutComponent, IWBaseContainerLayout, IWContainerLayout,
  IWTemplateProcessorHTML, IWCompEdit, IWCompCheckbox;

type
  TfrmPrincipal = class(TIWAppForm)
    IWRegion3: TIWRegion;
    btnCarregarArquivo: TIWButton;
    btnAtualizaLista: TIWButton;
    LabelTocandoAgora: TIWLabel;
    DataSource1: TDataSource;
    IWTemplateProcessorHTML1: TIWTemplateProcessorHTML;
    IWRegion1: TIWRegion;
    IWMPEG1: TIWMPEG;
    IWRegion2: TIWRegion;
    IWDBGrid1: TIWDBGrid;
    IWButton1: TIWButton;
    IWButton2: TIWButton;
    edtSearch: TIWEdit;
    IWButton3: TIWButton;
    cbxAutoPlay: TIWCheckBox;
    IWCheckBox1: TIWCheckBox;
    procedure btnAtualizaListaClick(Sender: TObject);
    procedure btnCarregarArquivoAsyncClick(Sender: TObject;
      EventParams: TStringList);
    procedure IWDBGrid1Columns0Click(ASender: TObject; const AValue: string);
    procedure IWDBGrid1RenderCell(ACell: TIWGridCell; const ARow,
      AColumn: Integer);
    procedure IWAppFormCreate(Sender: TObject);
    procedure IWButton1Click(Sender: TObject);
    procedure IWButton2Click(Sender: TObject);
    procedure IWAppFormAfterRender(Sender: TObject);
    procedure IWButton3Click(Sender: TObject);
    procedure edtSearchAsyncEnter(Sender: TObject; EventParams: TStringList);
    procedure IWCheckBox1Click(Sender: TObject);
  private
    FRowNo: Integer;
    procedure LoadFileList;
    procedure PlayCurrentRecord;
  public
    frmCarregaArquivo: TfrmCarregaArquivo;
  end;

implementation

uses StrUtils, ServerController;

{$R *.dfm}

function GetFileList(const Path: string; FileFilter: TStrings = Nil; const Recursive: Boolean = False; Const AddFolderName: Boolean = False; MaxFiles: Integer = -1): TStringList;
var
  FileList: TStringList;
  i: integer;

  procedure RFindFile(const folder: TFileName; FileExtension: string; const Recursive: Boolean);
  var
    SearchRec: TSearchRec;
  begin
    // Locate all matching files in the current
    // folder and add their names to the list
    if FindFirst(folder + FileExtension, faAnyFile, SearchRec) = 0 then begin
      try
        repeat
          Application.ProcessMessages;
          if (SearchRec.Attr and faDirectory = 0) or (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then begin
            FileList.Add(IfThen(AddFolderName, folder, '') + SearchRec.Name);
            if (MaxFiles <> -1) and (FileList.Count >= MaxFiles) then
              Break;
          end;
        until (FindNext(SearchRec) <> 0);
      except
        SysUtils.FindClose(SearchRec);
        raise;
      end;
      SysUtils.FindClose(SearchRec);
    end;
    if Recursive then begin
      // Now search the subfolders
      if FindFirst(folder + '*', faAnyFile, SearchRec) = 0 then begin
        try
          repeat
            if ((SearchRec.Attr and faDirectory) <> 0) and
              (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
              RFindFile(folder + SearchRec.Name + '\', FileExtension, Recursive);
          until (FindNext(SearchRec) <> 0);
        except
          SysUtils.FindClose(SearchRec);
          raise;
        end;
        SysUtils.FindClose(SearchRec);
      end;
    end;
  end; // procedure RFindFile inside of FindFile

begin // function FindFile
  FileList := TStringList.Create;
  try
    for i := 0 to FileFilter.Count - 1 do
      RFindFile(IncludeTrailingPathDelimiter(Path), FileFilter[i], Recursive);
    Result := FileList;
  except
    FileList.Free;
    raise;
  end;
end;

procedure TfrmPrincipal.btnAtualizaListaClick(Sender: TObject);
begin
  ;
end;

procedure TfrmPrincipal.btnCarregarArquivoAsyncClick(Sender: TObject;
  EventParams: TStringList);
begin
  if not Assigned(frmCarregaArquivo) then
    frmCarregaArquivo := TfrmCarregaArquivo.Create(Self);
  frmCarregaArquivo.Show;
end;

procedure TfrmPrincipal.edtSearchAsyncEnter(Sender: TObject;
  EventParams: TStringList);
begin
  edtSearch.Text := '';
end;

procedure TfrmPrincipal.IWAppFormAfterRender(Sender: TObject);
begin
  IWMPEG1.MPEGFile.FileName := '';
  LoadFileList;
  UserSession.Dataset.Locate('RowNo', FRowNo, []);
end;

procedure TfrmPrincipal.IWAppFormCreate(Sender: TObject);
begin
  FRowNo := -1;
  LoadFileList;
end;

procedure TfrmPrincipal.IWButton1Click(Sender: TObject);
begin
  UserSession.Dataset.MoveBy(-IWDBGrid1.RowLimit);
end;

procedure TfrmPrincipal.IWButton2Click(Sender: TObject);
begin
  UserSession.Dataset.MoveBy(+IWDBGrid1.RowLimit);
end;

procedure TfrmPrincipal.IWButton3Click(Sender: TObject);
var
  Located: Boolean;
begin
  FRowNo := UserSession.Dataset.FieldByName('RowNo').AsInteger;
  UserSession.Dataset.Locate('RowNo', FRowNo, []);
  with UserSession.Dataset do begin
    First;
    while not EOF do begin
      Located := Pos(UpperCase(edtSearch.Text), UpperCase(FieldByName('filename').AsString)) <> 0;
      if Located then begin
        if cbxAutoPlay.Checked then begin
          PlayCurrentRecord;
        end;
        Break;
      end;
      Next;
    end;
  end;
  if not Located then
    UserSession.Dataset.Locate('RowNo', FRowNo, []);
  edtSearch.Text := 'Type text to search';
end;

procedure TfrmPrincipal.IWCheckBox1Click(Sender: TObject);
begin
  IWTemplateProcessorHTML1.Enabled := not IWCheckBox1.Checked;
  StyleSheet.Filename := IfThen(not IWCheckBox1.Checked, 'files\styles.css', '');
end;

procedure TfrmPrincipal.IWDBGrid1Columns0Click(ASender: TObject; const AValue: string);
begin
  FRowNo := StrToInt(AValue);
  UserSession.Dataset.Locate('RowNo', FRowNo, []);
  PlayCurrentRecord;
end;

procedure TfrmPrincipal.IWDBGrid1RenderCell(ACell: TIWGridCell; const ARow,
  AColumn: Integer);
begin
  // shows the indicator of the current row (selected row)
  if IWDBGrid1.RowIsCurrent and (AColumn = 0) then begin
    if ARow mod 2 = 1 then
      ACell.Css := 'selected'
    else
      ACell.Css := 'selectedalt';
    Exit;
  end;

  if (ARow mod 2 = 0) then
    ACell.Css := 'alt';

  if AColumn = 0 then begin
    if (ARow mod 2 = 1) then
      ACell.Css := 'spec'
    else
      ACell.Css := 'specalt'
  end;

end;

procedure TfrmPrincipal.LoadFileList;
var
  FileList: TStringList;
  FileFilter: TStringList;
  i: Integer;
begin
  FileFilter := TStringList.Create;
  FileFilter.Add('*.mp3');
  FileFilter.Add('*.wmv');
  try
    UserSession.Dataset.EmptyDataSet;
    FileList := GetFileList('files', FileFilter);
    for i := 0 to FileList.Count - 1 do begin
      with UserSession.Dataset do begin
        Insert;
        FieldByName('FileName').AsString := FileList[i];
        FieldByName('RowNo').AsInteger := i + 1;
        Post;
      end;
    end;
    DataSource1.DataSet := UserSession.Dataset;
    UserSession.Dataset.First
  finally
    FileFilter.Free;
    if Assigned(FileList) then
      FileList.Free;
  end;
end;

procedure TfrmPrincipal.PlayCurrentRecord;
begin
  IWMPEG1.MPEGFile.FileName := 'files\' + UserSession.Dataset.FieldByName('FileName').AsString;
  IWMPEG1.AutoStart := True;
  LabelTocandoAgora.Caption := 'You are playing ' + UserSession.Dataset.FieldByName('FileName').AsString;
end;

initialization
  TfrmPrincipal.SetAsMainForm;

end.
